/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski   *
 *   g.latarowski@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifndef ROIITEM_H
#define ROIITEM_H


// Qt
#include <QtGui>

// lib
#include "gxmlelement.h"

// local
#include "graph.h"


// ---------------------------- GRoi{} ---------------------------------------
/// \brief Region Of Interest item editor.
///
/// Its a graph based editor with nodes and edges. There is ability to move
/// nodes within scenes rect and setup correct position of roi. Nodes can be
/// created and removed.
// ---------------------------------------------------------------------------
class GRoi : public GGraph, public GXmlElement
{
	Q_OBJECT
	
public:

	// ====================================================
	// constructor

	/// \brief Contructs a roi item.
	///
	/// GGraph constructor is called. Roi is constructer with no nodes. 
	GRoi( QGraphicsItem * pParent = 0, QGraphicsScene * pScene = 0 );
	
	/// \brief Deletes roi item.
	///
	/// Every node and edge is deleted.
	~GRoi();

	
	// ====================================================
	// xml element

	virtual QDomElement toXml( QDomDocument & doc )const;
	
	virtual void fromXml( const QDomElement & element );

	
	// ====================================================
	// roi specific 

	/// \brief Set items \e zValue to \a z.
	///
	/// It sets every items node zValue to z+0.02
	/// and edge zValue to z+0.01
	void setZValue( const qreal z );

	/// \brief Adjusts rois position.
	///
	/// Calls edges to update their positions.
	void adjust();

	/// \brief Creates new nodes on the \a positions.  
	void setNodes( const QPolygonF & positions );

	/// \brief Returns a polygon with each nodes position.
	QPolygonF nodes()const;

	/// \brief Removes \a node from _nodes
	void removeNode( GNode * node );

	/// \brief Returns roi's color.
	QColor color() const { return _color; }

	/// \brief Adds node after \a previousNode at the \a position.
	virtual void addNode( GNode * previousNode, const QPointF & position );

	/// \brief Returns path that covers roi. 
	QPainterPath roiRegion()const;
	

	// ====================================================
	// QGraphics specific 

	/// \brief Returns rois bounding rect. Same as its scene rect.
	virtual QRectF boundingRect() const;

	/// \brief Paints roi item.
	virtual void paint( QPainter * pPainter,
						const QStyleOptionGraphicsItem * pOption,
						QWidget * widget );

	
private:
	/// \brief Creates new edges based on \e _nodes vector.
	void updateEdges();
	
	QVector< GNode * > _nodes; ///< Vector with pointer to items nodes 
	QVector< GEdge * > _edges; ///< Vector with pointer to items edges 
	
	QPolygonF _roi;            ///< Nodes positions polygon 
	
	QRectF _boundingRect;      ///< Item bounding rect. Same as scene rect
	QColor _color;             ///< Color used to paint roi item
	
}; // class !GRoi
// ---------------------------------------------------------------------------





#endif // !ROIITEM_H






// eof
